<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="listQuery.parkingNo" placeholder="蓄车场" clearable class="filter-item" style="width: 120px;">
        <el-option v-for="item in parkingNoOptions" :key="item.key" :label="item.label" :value="item.key" />
      </el-select>
      <el-select v-model="listQuery.inuse" placeholder="使用状态" clearable class="filter-item" style="width: 120px;">
        <el-option v-for="item in inuseOptions" :key="item.key" :label="item.label" :value="item.key" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      <!--      <el-button v-if="type == 0" v-waves class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">新增车场车道</el-button> -->
    </div>

    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="蓄车场" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.parkingNo | parkingNoFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="科拓车道号" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.nodeId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="科拓车场号" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.keytopParkId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="显示屏IP" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.screenIP }}</span>
        </template>
      </el-table-column>
      <el-table-column label="主相机IP" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.mainCameraIP }}</span>
        </template>
      </el-table-column>
      <el-table-column label="辅相机IP" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.assistCameraIP }}</span>
        </template>
      </el-table-column>
      <el-table-column label="使用状态" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.inuse | inuseFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="type == 0" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{ row }">
          <el-button size="mini" icon="el-icon-edit" type="primary" @click="handleEdit(row)">编辑</el-button>
          <!--          <el-button size="mini" icon="el-icon-delete" type="danger" @click="handleDelete(row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />
    <div v-if="dialogAddFormVisible">
      <el-dialog title="新增车场车道" :visible.sync="dialogAddFormVisible" :append-to-body="true" :close-on-click-modal="false" :destroy-on-close="true">
        <ParkingAddForm ref="parkingAddForm" :length="total" @close="parkingAddFormClose" @cancelClose="cancelClose(arguments)" />
      </el-dialog>
    </div>
    <div v-if="dialogEditFormVisible">
      <el-drawer :visible.sync="dialogEditFormVisible" direction="rtl" size="85%" :append-to-body="true" :destroy-on-close="true" :wrapper-closable="false" :with-header="false">
        <ParkingAddForm :id="editId" ref="parkingEditForm" @cancelClose="cancelClose(arguments)" />
      </el-drawer>
    </div>
  </div>
</template>

<script>
import { getSysParking, deleteSysParking } from '@/api/parking';
import waves from '@/directive/waves';
import Pagination from '@/components/Pagination';
import ParkingAddForm from '@/views/sys/parkingAdd.vue';

var that;

export default {
  name: 'SysParking',
  components: { Pagination, ParkingAddForm },
  directives: { waves },
  filters: {
    parkingNoFilter(item) {
      const parkingNoKeyValue = that.parkingNoOptions.reduce((acc, cur) => {
        acc[cur.key] = cur.label;
        return acc;
      }, {});
      return parkingNoKeyValue[item];
    },
    inuseFilter(item) {
      const inuseKeyValue = that.inuseOptions.reduce((acc, cur) => {
        acc[cur.key] = cur.label;
        return acc;
      }, {});
      return inuseKeyValue[item];
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        parkingNo: null,
        inuse: null
      },
      parkingNoOptions: [{ label: '南蓄', key: 0 }, { label: '北蓄', key: 1 }],
      inuseOptions: [{ label: '否', key: 0 }, { label: '是', key: 1 }],
      dialogAddFormVisible: false,
      dialogEditFormVisible: false,
      editId: 0,
      type: null
    };
  },
  created() {
    that = this;
    const { params } = this.$route
    Object.keys(params).forEach(key => {
      this.type = params[key].split(':')[1];
    });
    this.getList();
  },
  methods: {
    async getList() {
      this.listLoading = true;
      await getSysParking(this.listQuery).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    async handleSearch() {
      this.getList();
    },
    handleCreate() {
      this.dialogAddFormVisible = true;
    },
    async handleDelete(row) {
      await this.$confirm('确定要此条数据吗？', '刪除操作', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          this.listLoading = true;
          await deleteSysParking(row.id).then(res => {
            this.listLoading = false;
            if (res.msg !== 'OK') {
              this.$message.error(res.msg);
              return;
            }
            this.getList();
            this.$message.success('删除成功');
          });
        })
        .catch(() => {});
    },
    handleEdit(row) {
      this.editId = row.id;
      this.dialogEditFormVisible = true;
    },
    parkingAddFormClose() {
      this.dialogAddFormVisible = false;
    },
    cancelClose(data) {
      if (data[1] === 'add') {
        this.dialogAddFormVisible = false;
      } else {
        this.dialogEditFormVisible = false;
      }
      if (data[0]) {
        if (data[1] === 'add') {
          this.listQuery.pageNum = 1;
          this.listQuery.pageSize = 10;
          this.listQuery.parkingNo = null;
          this.listQuery.inuse = null;
        }
        this.getList();
      }
    }
  }
};
</script>
